package com.china08.yxyapi.api.comm;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.util.SpringContextUtil;

/**
 * 
 * @annotation: 更新微课封面
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class WeikeCoverUptController {

    @Autowired
    private YxyResResourceRepository yrrr;

    private static final String ES_RES_RESOURCE_SINGLE_QUEUE = "es-res-resource-single-queue";

    @RequestMapping(value = "/api_yw/comm/weike/cover/upd", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        RespModel respModel = new RespModel();
        Page<YxyResResource> page = null;
        int i = 0;
        // 默认图片
        List<String> imgList = new ArrayList<String>();
        imgList.add("https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/73e1e1cb5602499c61bece109dffb6fe.jpg");
        imgList.add("https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/4fccaaaed4b25fad8c529f0b9ae7ce23.jpg");
        imgList.add("https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/869f888d71dd0cc92b343579fd25aa15.jpg");
        imgList.add("https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/81a218e1ed8853fb708e1f3c3bd19236.jpg");
        imgList.add("https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/8d6e3197b794847bb587087a34954b32.jpg");
        List<String> resIdList = new ArrayList<String>();
        // 替换封面
        page = this.UpdateRes(i);
        while (page.getContent().size() > 0) {
            i += 1;
            for (YxyResResource resource : page) {
                String newCover = imgList.get(new Random().nextInt(4));
                resIdList.add(resource.getId());
                String cover = resource.getCover();
                if (cover != null) {
                    if (cover.contains("yxcsp.91yunxiao.com/yxcsp/pull/811624024c1a8ed19a91886439d84b42.jpg")
                            || cover.contains("yxcsp.91yunxiao.com/yxcsp/pull/a9cc53a8f881d96b60cf324ba8acce6c.jpg")
                            || cover.contains("yxcsp.91yunxiao.com/yxcsp/pull/5851661ae0f28fe98a4a8a98f45a0ce1.jpg")
                            || cover.contains("yxcsp.91yunxiao.com/yxcsp/pull/bbcdd670b532318bb4c4193dcb0a66fc.jpg")
                            || cover.contains("yxcsp.91yunxiao.com/yxcsp/pull/705e22e6faf771b07c536b7ad5c7dae8.jpg")) {

                        resource.setCover(newCover);
                    }
                }
            }
            yrrr.save(page);
            page = this.UpdateRes(i);

        }
        // 同步ES
        for (String resId : resIdList) {
            try {
                RabbitTemplate template = SpringContextUtil.getBean(RabbitTemplate.class);
                if (StringUtils.isNotBlank(resId)) {
                    template.convertAndSend(ES_RES_RESOURCE_SINGLE_QUEUE, resId.trim());
                }
            } catch (Exception e) {
            }
        }
        respModel.setMessage("更新成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    public Page<YxyResResource> UpdateRes(Integer i) {
        Pageable pageable = null;
        Page<YxyResResource> page = null;
        pageable = new PageRequest(i, 1000, Direction.ASC, "createdDate");
        page = yrrr.findByTypeId("yx14", pageable);
        return page;
    }

    public class RespModel extends YxyApiRespModel {

    }
}
